package com.qqtech.qquan.mall.service;

import java.math.BigDecimal;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.qqtech.core.frame.dao.BaseDao;
import com.qqtech.core.frame.service.BaseServiceImpl;
import com.qqtech.qquan.mall.cache.MallSysParamCache;
import com.qqtech.qquan.mall.dao.MallCartDao;
import com.qqtech.qquan.mall.model.MallCart;
import com.qqtech.qquan.mall.model.MallCartQuery;
import com.qqtech.qquan.mall.model.MallCartVo;

@Service
public class MallCartServiceImpl extends BaseServiceImpl<MallCart>
		implements MallCartService {
	public static final Logger log = LoggerFactory.getLogger(MallCartServiceImpl.class);
	
	@Resource
	private MallCartDao mallCartDao;
    @Resource
    private MallSysParamCache mallSysParamCache;
    
	@Override
	protected BaseDao<MallCart> getBaseDao() {
		return mallCartDao;
	}

	@Override
	public int handleAddProduct2Cart(MallCart cart) {
		int id = mallCartDao.insert(cart);
		return id;
	}

	@Override
	public int handleDeleteProduct(int userId, int productId) {
		MallCart mallCart = new MallCart();
		mallCart.setCreatorId(userId);
		mallCart.setProductId(productId);
		int id = mallCartDao.delete(mallCart);
		return id;
	}

	@Override
	public int updateQuantity(int userId, Integer productId, int quantity) {
		MallCart mallCart = new MallCart();
		mallCart.setCreatorId(userId);
		mallCart.setProductId(productId);
		mallCart.setQuantity(quantity);
		int id = mallCartDao.updateById(mallCart);
		return id;
	}

	@Override
	public int getTotalQuantity(Integer userId, Integer shopId) {
		return mallCartDao.getTotalQuantity(userId, shopId);
	}

	@Override
	public BigDecimal getTotalPrice(Integer userId, Integer shopId) {
		return mallCartDao.getTotalPrice(userId, shopId);
	}

	@Override
	public Page<MallCartVo> queryPageBySupplier(MallCartQuery query,
			Pageable pageable) {
		return mallCartDao.queryPageBySupplier(query, pageable);
	}
	
	@Override
	public Page<MallCartVo> queryPageByShop(MallCartQuery query,
			Pageable pageable) {
		return mallCartDao.queryPageByShop(query, pageable);
	}

	@Override
	public int queryCountBySupplier(MallCartQuery query) {
		return mallCartDao.queryCountBySupplier(query);
	}
	
	@Override
	public int queryCountByShop(MallCartQuery query) {
		return mallCartDao.queryCountByShop(query);
	}

	@Override
	public String getDeliverTime(String key) {
		return mallSysParamCache.get(key);
	}

}